/**
 * Created by Administrator on 2017/8/3.
 */
$(document).ready(function () {

    var allAddressTpl=$('#all-address-template').html();
    var allAddressCmp=Handlebars.compile(allAddressTpl);
    $.ajax({
        url:C.interface.allAddress,
        dataType:'json',
        type:'get',
        data:{
            token:C.token
        },
        success:function (response) {
            if (response.result=='success'){
                var data=response.data;
                var count=data.count;

                if (count === '0'){
                    $('.none-address').show();
                }else {
                    $('#address-list-box').html(allAddressCmp(data));
                    //$('.ab').bind('click',insertAddressId);
                    $('.address-delete').bind('click',deleteAddress);
                    $('.address-set-default').bind('click',setDefault);
                    $('.address-edit').bind('click',function () {
                        location.href='insertaddress.html';
                    });
                }

            }else if (response.result=='login'){
                alert('登录过期请重新登录');
            }else {
                alert(response.errorMsg);
            }
        }
    });

    //设置为默认收货地址

    function setDefault() {
        var addressId=$(this).parent('.address-select').data('address-id');
        $.ajax({
            url:C.interface.changeAddress,
            type:'get',
            dataType:'json',
            data:{
                id:addressId,
                token:C.token
            },
            success:function (response) {
                if (response.result === 'success'){
                    console.log('修改成功');
                }else if (response.result === 'login'){
                    alert('登录过期，请重新登录');
                    againLogin();
                }else {
                    alert(response.errorMsg);
                }
            }
        })
    }



    //删除收货地址
    function deleteAddress() {
        var that=$(this);
        dialog.confirm({
            title:'确定要删除吗？',
            content:'',
            ok:function () {
                var addressId=that.parent('.address-select').data('address-id');
                $.ajax({
                    url:C.interface.deleteAddress,
                    type:'get',
                    dataType:'json',
                    data:{
                        token:C.token,
                        id:addressId
                    },
                    success:function (response) {
                        if (response.result=='success'){

                            that.parents('.address-box').remove();

                            noneAddress();

                            dialog.tusiSuccess('删除成功！');

                        }else if (response.result=='login'){
                            alert('登录过期请重新登录');
                        }else {
                            alert(response.errorMsg);
                        }
                    }
                });
            },
            cancel: function () {

            }
        });

    }

    //判断当前是否还有收货地址，没有，则显示无收货地址。
    function noneAddress() {
        var addressLenth=$('.address-box').length;
        if (addressLenth === 0){
            $('#address-list-box').append("<li class='none-address'>暂无收货地址 .... </li>");
            $('.none-address').show();
        }
    }







    /*Handlebars.registerHelper('defaultAddress',function (value) {
        if (value)
    })*/
});